package cn.sirun.com.fca.account.view.impl;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.PatternMatchingUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.SRPUtils;
import com.tencent.cloud.iov.util.lifecycle.ActivityStackManager;
import com.tencent.cloud.uikit.utils.SystemUiUtils;
import com.tencent.cloud.uikit.widget.edittext.LineEditText;
import com.tencent.cloud.uikit.widget.edittext.PasswordEditText;

import cn.sirun.com.fca.account.R;
import cn.sirun.com.fca.account.model.login.ChallengeResponse;
import cn.sirun.com.fca.account.presenter.IConfirmPasswordPresenter;
import cn.sirun.com.fca.account.presenter.impl.ConfirmPasswordPresenter;
import cn.sirun.com.fca.account.uitl.AccountStatusUtils;
import cn.sirun.com.fca.account.view.IModifyPassView;

public class ModifyPassFlowView extends BaseLoadingFlowView<IConfirmPasswordPresenter>
        implements IModifyPassView {

    private PasswordEditText mPassET, mRePassET;
    private ImageView mPromptIV, mRePromptIV;
    private TextView mPromptTV, mRePromptTV, mSubmitTV;
    private View mLineV, mReLineV;

    public ModifyPassFlowView(Activity activity) {
        super(activity);
    }

    public ModifyPassFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setUpActionBar();
        initView();
    }

    private void setUpActionBar() {
        final Activity activity = getActivity();
        SystemUiUtils.showActionBar(activity, "");
    }

    @Override
    public void onChallengeResult(BaseResponse<ChallengeResponse> response) {
        int code = response.getCode();
        if (StatusCode.SUCCESS == code) {
            ChallengeResponse challengeResponse = response.getData();
            String salt = challengeResponse.getSalt();
            String group = challengeResponse.getGroup();
            sendSetPasswordRequest(salt, group);
        } else {
            hideLoadingView();
            AccountStatusUtils.showToast(getActivity(), code);
        }
    }

    private void sendSetPasswordRequest(String salt, String group) {
        String password = mPassET.getText().toString();
        getPresenter().setPassword(SRPUtils.encryptData(password, salt, group));
    }

    @Override
    public void onloginePasswordResult(BaseResponse<Void> response) {
        hideLoadingView();
        int code = response.getCode();
        if (StatusCode.SUCCESS == response.getCode()) {
            ActivityStackManager.getInstance().clearNumberStack(2);
        } else {
            AccountStatusUtils.showToast(getActivity(), code);
        }
    }

    @Override
    protected IConfirmPasswordPresenter createPresenter() {
        return new ConfirmPasswordPresenter();
    }

    private void initView() {
        setContentView(R.layout.modify_pass_activity);
        final Activity activity = getActivity();
        mPassET = activity.findViewById(R.id.et_pass_number);
        mRePassET = activity.findViewById(R.id.et_repass_number);
        mPromptIV = activity.findViewById(R.id.iv_pass_prompt);
        mRePromptIV = activity.findViewById(R.id.iv_repass_prompt);
        mRePromptTV = activity.findViewById(R.id.tv_repass);
        mPromptTV = activity.findViewById(R.id.tv_pass);
        mSubmitTV = activity.findViewById(R.id.tv_modify_pass);
        mLineV = activity.findViewById(R.id.v_pass_line);
        mReLineV = activity.findViewById(R.id.v_repass_line);
        registerListener();
    }

    private void registerListener() {
        mPassET.setCheckFormatListener(new LineEditText.OnCheckFormatListener() {
            @Override
            public void onSuccess() {
                setPassLayoutBg(View.GONE, ResourcesUtils.getColor(cn.sirun.com.fca.account.R.color.grey_A9),
                        ResourcesUtils.getString(cn.sirun.com.fca.account.R.string.tv_title_input_pass), cn.sirun.com.fca.account.R.color.divider_bg_color);
            }

            @Override
            public void onFailed() {
                setPassLayoutBg(View.VISIBLE, ResourcesUtils.getColor(cn.sirun.com.fca.account.R.color.login_register_text_color),
                        ResourcesUtils.getString(cn.sirun.com.fca.account.R.string.tv_pass_notice), cn.sirun.com.fca.account.R.color.login_register_text_color);
            }
        });

        mPassET.setTextChangeListener(new LineEditText.OnTextChangeListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setConfirmBtnEnable();
            }
        });

        mRePassET.setTextChangeListener(new LineEditText.OnTextChangeListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setConfirmBtnEnable();
            }
        });

        mSubmitTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkFormat()) {
                    return;
                }
                showLoadingView();
                getPresenter().getSetParams();
            }
        });

    }

    private void setPassLayoutBg(int visibility, int textColor, String text, int lineColor) {
        mPromptIV.setVisibility(visibility);
        mPromptTV.setTextColor(textColor);
        mPromptTV.setText(text);
        mLineV.setBackgroundResource(lineColor);
    }

    private void setRePassLayoutBg(int visibility, int textColor, String text, int lineColor) {
        mRePromptIV.setVisibility(visibility);
        mRePromptTV.setTextColor(textColor);
        mRePromptTV.setText(text);
        mReLineV.setBackgroundResource(lineColor);
    }

    private void setConfirmBtnEnable() {

        int passwordLength = mPassET.length();
        int reenterPasswordLength = mRePassET.length();

        boolean isEnable = (passwordLength >= PasswordEditText.MIN_LENGTH
                && reenterPasswordLength >= PasswordEditText.MIN_LENGTH);

        if (0 == reenterPasswordLength) {
            setRePassLayoutBg(View.GONE, ResourcesUtils.getColor(R.color.grey_A9),
                    ResourcesUtils.getString(R.string.new_login_password), cn.sirun.com.fca.account.R.color.divider_bg_color);
        }

        if (0 == passwordLength) {
            setPassLayoutBg(View.GONE, ResourcesUtils.getColor(R.color.grey_A9),
                    ResourcesUtils.getString(R.string.new_login_password), cn.sirun.com.fca.account.R.color.divider_bg_color);
        }

        if (isEnable) {
            mSubmitTV.setBackgroundResource(cn.sirun.com.fca.account.R.drawable.s_btn_bg_border);
        } else {
            mSubmitTV.setBackgroundResource(cn.sirun.com.fca.account.R.drawable.s_btn_default_bg_border);
        }
        mSubmitTV.setEnabled(isEnable);
    }

    private boolean checkFormat() {
        String password = mPassET.getText().toString();
        String reEnterPassword = mRePassET.getText().toString();
        boolean isInputPasswordCorrect = PatternMatchingUtils.isPasswordCorrect(password);

        if (isInputPasswordCorrect) {
            if (password.equals(reEnterPassword)) {
                setRePassLayoutBg(View.GONE, ResourcesUtils.getColor(cn.sirun.com.fca.account.R.color.grey_A9),
                        ResourcesUtils.getString(R.string.new_login_password), cn.sirun.com.fca.account.R.color.divider_bg_color);
                return true;
            } else {
                setRePassLayoutBg(View.VISIBLE, ResourcesUtils.getColor(cn.sirun.com.fca.account.R.color.login_register_text_color),
                        ResourcesUtils.getString(cn.sirun.com.fca.account.R.string.tv_title_input_pass_error), cn.sirun.com.fca.account.R.color.login_register_text_color);
                return false;
            }
        } else {
            setPassLayoutBg(View.VISIBLE, ResourcesUtils.getColor(cn.sirun.com.fca.account.R.color.login_register_text_color),
                    ResourcesUtils.getString(cn.sirun.com.fca.account.R.string.tv_pass_notice), cn.sirun.com.fca.account.R.color.login_register_text_color);
            return false;
        }
    }

}
